//
//  ImageSplicingTemplateViewController.m
//  TSImageFilterDemo
//
//  Created by ciyouzen on 2017/2/25.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "ImageSplicingTemplateViewController.h"
#import <Masonry/Masonry.h>
#import "GLMeituContentView.h"
#import "GLStoryboardSelectView.h"

@interface ImageSplicingTemplateViewController () <GLMeituContentViewDelegate, GLStoryboardSelectViewDelegate> {
    
}
@property (nonatomic, strong) GLMeituContentView *templateContentView;          /**< 模板上的所有图片的展示视图 */
@property (nonatomic, strong) GLStoryboardSelectView *templateStoryboardView;   /**< 拼接的样式选择 */

@property (nonatomic, strong, readonly) NSArray<UIImage *> *assets;    /**< 要处理的图片 */

@end

@implementation ImageSplicingTemplateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupViews];
    
    NSArray<UIImage *> *images = @[[UIImage imageNamed:@"bmp1.bmp"],
                                   [UIImage imageNamed:@"bmp2.bmp"],
                                   [UIImage imageNamed:@"image1.jpg"],
                                   [UIImage imageNamed:@"image2.jpg"],
                                   [UIImage imageNamed:@"0.png"],
                                ];
    _assets = images;
    
    
    [self initToolbarViewWithCount:self.assets.count];

    [self.templateContentView setAssets:self.assets];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self didSelectedStoryboardPicCount:self.assets.count styleIndex:1];
    });
}

#pragma mark - setupViews
- (void)setupViews {
    self.view.backgroundColor = [UIColor blackColor];
    
    GLMeituContentView *templateContentView = [[GLMeituContentView alloc] initWithFrame:CGRectZero];
    templateContentView.delegateMove = self;
    [self.view addSubview:templateContentView];
    [templateContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_equalTo(20);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-100);
    }];
    self.templateContentView = templateContentView;
}


- (void)initToolbarViewWithCount:(NSInteger)picCount {
    CGFloat storyboardViewWidth = self.view.frame.size.width;
    CGFloat storyboardViewHeight = 50;
    GLStoryboardSelectView *templateStoryboardView = [[GLStoryboardSelectView alloc] initWithFrame:CGRectMake(0, 0, storyboardViewWidth, storyboardViewHeight) picCount:picCount];
    [templateStoryboardView setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.6]];
    templateStoryboardView.delegateSelect = self;
    [self.view addSubview:templateStoryboardView];
    [templateStoryboardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.height.mas_equalTo(storyboardViewHeight);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-0);
    }];
    self.templateStoryboardView = templateStoryboardView;
}




#pragma mark - GLStoryboardSelectViewDelegate：分镜的选择
/**
 *  分镜的选择
 *
 *  @param sender
 */
- (void)didSelectedStoryboardPicCount:(NSInteger)picCount styleIndex:(NSInteger)styleIndex {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self resetViewByStyleIndex:styleIndex imageCount:self.assets.count];
    });
}

- (void)resetViewByStyleIndex:(NSInteger)index imageCount:(NSInteger)count {
    @synchronized(self){
        [self.templateContentView setStyleIndex:index];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
